/*
 *  LineTest.cpp
 *  SoundController0002
 *
 *  Created by jiro on 10/09/20.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#include "LineTester.h"
#include "../LineFactory.h"
#include "CustomAssert.h"

namespace sc 
{
	void LineTester::TestAll()
	{
		Test_0001();
		
		Test_0002();

		Test_0003();

		Test_0004();

	}

	void LineTester::Test_0001()
	{
		const double maxDynamics = 1.0;
		const double attackTime = 0.2;
		const double decayTime = 0.2;
		const double releaseTime = 0.2;
		const double noteLength = 4;
		const double attackEndTime = attackTime;
		const double decayEndTime = attackEndTime + decayTime;
		const double sustainEndTime = decayEndTime + noteLength - attackTime - decayTime - releaseTime;
		const double releaseEndTime = sustainEndTime + releaseTime;

		const double attackDynamics = maxDynamics;
		const double decayDynamics = 0.75 * maxDynamics;
		const double sustainDynamics = decayDynamics;

		const Line& attackLine	= LineFactory::Create( Line::Type_Straight, 0.0,            0.0,             attackEndTime,  attackDynamics );
		const Line& decayLine	= LineFactory::Create( Line::Type_Straight, attackEndTime,  attackDynamics,  decayEndTime,   decayDynamics );
		const Line& sustainLine = LineFactory::Create( Line::Type_Straight, decayEndTime,   decayDynamics,	 sustainEndTime, sustainDynamics );
		const Line& releaseLine = LineFactory::Create( Line::Type_Straight, sustainEndTime,	sustainDynamics, releaseEndTime, 0.0 );
		
		double x = 0.0;
		double y;
		
		y = attackLine.GetY( 0.0 );
		AssertEqual( y, 0.0 );
		
		x = attackEndTime;
		y = attackLine.GetY( x );
		AssertEqual( y, attackDynamics );
		
		y = decayLine.GetY( x );
		AssertEqual( y, attackDynamics );
		
		x = decayEndTime;
		y = decayLine.GetY( x );
		AssertEqual( y, decayDynamics );
		
		y = sustainLine.GetY( x );
		AssertEqual( y, decayDynamics );
		
		x = sustainEndTime;
		y = sustainLine.GetY( x );
		AssertEqual( y, sustainDynamics );
		
		y = releaseLine.GetY( x );
		AssertEqual( y, sustainDynamics );
		
		x = releaseEndTime;
		y = releaseLine.GetY( x );
		AssertEqual( y, 0.0 );

	}
	
	void LineTester::Test_0002()
	{
		const Line& line = LineFactory::Create( Line::Type_ConvexParabola, 0, 0, 1, 1 );
		AssertEqual( line.GetY( 0 ), 0 );
		AssertEqual( line.GetY( 1 ), 1 );
		AssertTrue( line.GetY( 0.5 ) < 1 );
		AssertTrue( line.GetY( 0.5 ) > 0 );
//		std::cout << line.ToString() << std::endl;
	}
	
	void LineTester::Test_0003()
	{
		const Line& line = LineFactory::Create( Line::Type_ConcateParabola, 0, 0, 1, 1 );
		AssertEqual( line.GetY( 0 ), 0 );
		AssertEqual( line.GetY( 1 ), 1 );
		AssertTrue( line.GetY( 0.5 ) < 1 );
		AssertTrue( line.GetY( 0.5 ) > 0 );
	}

	
	void LineTester::Test_0004()
	{
		const Line& line = LineFactory::Create( Line::Type_ConvexParabola, 1, 0, 2, 1 );
		AssertEqual( line.GetY( 1 ), 0 );
		AssertEqual( line.GetY( 2 ), 1 );
		AssertTrue( line.GetY( 1.5 ) < 1 );
		AssertTrue( line.GetY( 1.5 ) > 0 );
//		std::cout << line.ToString() << std::endl;
	}

}